set -e
set -o pipefail

if [ -z "${MIN_INTERPEAKS}" ]; then MIN_INTERPEAKS=3; fi
if [ -z "${CL_START_NM}" ]; then CL_START_NM=15.0; fi
if [ -z "${CL_END_NM}" ]; then CL_END_NM=23.0; fi
if [ -z "${FORCE_LOW_HIGH_THRESHOLD}" ]; then FORCE_LOW_HIGH_THRESHOLD=1.0; fi
if [ -z "${OUTLIER_TEST}" ]; then OUTLIER_TEST=iqr2; fi
if [ -z "${KEEP_SUBSTRETCHES}" ]; then KEEP_SUBSTRETCHES=True; fi
if [ -z "${MIN_DETACHMENT_FORCE}" ]; then MIN_DETACHMENT_FORCE=200.0; fi
if [ -z "${DETACHMENT_STRICTLY_LARGEST}" ]; then DETACHMENT_STRICTLY_LARGEST="True"; fi
if [ -z "${MIN_DELTA_CL_NM}" ]; then MIN_DELTA_CL_NM=0; fi
if [ -z "${MAX_DELTA_CL_NM}" ]; then MAX_DELTA_CL_NM=50; fi
if [ -z "${EPS2PDF}" ]; then EPS2PDF="ps2pdf"; fi

fixedDeflectionSensitivityNmPerV=$DEFL_SENS; fixedSpringConstantNPerM=$K; kineticsSuffix=${KINETICS_SUFFIX//*/_${KINETICS_SUFFIX}}; for d in $DIRS; do p=$(dirname ${d}); b=$(basename ${d})${fixedDeflectionSensitivityNmPerV//*/_${fixedDeflectionSensitivityNmPerV}}${fixedSpringConstantNPerM//*/_${fixedSpringConstantNPerM}}; cd ~/work/rcs/refolding-read-only/core/src/main/python && python2.7 delta_cl_stats.py --processed_playlist=${p}/analysis/${b}_analysis/${b}.repr --curve_stretch_playlist= --min_delta_cl_nm=${MIN_DELTA_CL_NM} --max_delta_cl_nm=${MAX_DELTA_CL_NM} --min_rupture_force_pn=0 --max_rupture_force_pn=400 --delta_force_filename=${p}/analysis/${b}_analysis/${b}_delta_force.csv --scatterplot_filename=${p}/analysis/${b}_analysis/${b}_scatter.eps && (cd ${p}/analysis/${b}_analysis && ${EPS2PDF} ${b}_scatter.eps) && python2.7 retraction_unfolding_filter.py --retraction_playlist=${p}/analysis/${b}_analysis/${b}.repr --cl_start_nm=${CL_START_NM} --cl_end_nm=${CL_END_NM} --min_interpeaks=${MIN_INTERPEAKS} --max_interpeaks=18 --detachment_strictly_largest=${DETACHMENT_STRICTLY_LARGEST} --minimum_detachment_force=${MIN_DETACHMENT_FORCE} --curve_stretch_playlist=${p}/analysis/${b}_analysis/filtered && python2.7 delta_cl_stats.py --processed_playlist=${p}/analysis/${b}_analysis/${b}.repr --curve_stretch_playlist=${p}/analysis/${b}_analysis/filtered_${DETACHMENT_STRICTLY_LARGEST}_${MIN_DETACHMENT_FORCE}_${MIN_INTERPEAKS}_${CL_START_NM}_${CL_END_NM}.curveStretchPlaylist --min_delta_cl_nm=${MIN_DELTA_CL_NM} --max_delta_cl_nm=${MAX_DELTA_CL_NM} --min_rupture_force_pn=0 --max_rupture_force_pn=10000 --delta_force_filename=${p}/analysis/${b}_analysis/${b}_stretch_delta_force.csv --scatterplot_filename=${p}/analysis/${b}_analysis/${b}_stretch_scatter.eps && (cd ${p}/analysis/${b}_analysis && ${EPS2PDF} ${b}_stretch_scatter.eps) && python2.7 curve_stretch_outliers.py --in_curve_stretch_playlist=${p}/analysis/${b}_analysis/filtered_${DETACHMENT_STRICTLY_LARGEST}_${MIN_DETACHMENT_FORCE}_${MIN_INTERPEAKS}_${CL_START_NM}_${CL_END_NM}.curveStretchPlaylist --outlier_test=${OUTLIER_TEST} --force_low_high_threshold=${FORCE_LOW_HIGH_THRESHOLD} --keep_substretches=${KEEP_SUBSTRETCHES} --out_curve_stretch_playlist=${p}/analysis/${b}_analysis/kinetics_${OUTLIER_TEST}_${FORCE_LOW_HIGH_THRESHOLD}_${KEEP_SUBSTRETCHES}.curveStretchPlaylist && python2.7 delta_cl_stats.py --processed_playlist=${p}/analysis/${b}_analysis/${b}.repr --curve_stretch_playlist=${p}/analysis/${b}_analysis/kinetics_${OUTLIER_TEST}_${FORCE_LOW_HIGH_THRESHOLD}_${KEEP_SUBSTRETCHES}${kineticsSuffix}.curveStretchPlaylist --min_delta_cl_nm=${MIN_DELTA_CL_NM} --max_delta_cl_nm=${MAX_DELTA_CL_NM} --min_rupture_force_pn=0 --max_rupture_force_pn=10000 --delta_force_filename=${p}/analysis/${b}_analysis/${b}_kinetics_stretch_delta_force${kineticsSuffix}.csv --scatterplot_filename=${p}/analysis/${b}_analysis/${b}_kinetics_stretch_scatter${kineticsSuffix}.eps && (cd ${p}/analysis/${b}_analysis && ${EPS2PDF} ${b}_kinetics_stretch_scatter${kineticsSuffix}.eps); done
